package com.xinruke.hostels.systemmanage.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.cache.extract.extractor.DictInfoCacheExtractor;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.cache.util.SysCodeUtil;
import com.xinruke.hostels.common.cache.vo.LoginManageResultVO;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.YwHuichang;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.system.manage.interfaces.dto.*;
import com.xinruke.hostels.systemmanage.mapper.YwHuichangMapper;
import com.xinruke.hostels.systemmanage.service.MeetingService;
import com.xinruke.hostels.systemmanage.service.MeetingTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yutaipu
 */
@Service
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    private MeetingTypeService meetingTypeService;

    @Autowired
    private YwHuichangMapper ywHuichangMapper;

    @Autowired
    private DictInfoCacheExtractor dictInfoCacheExtractor;

    @Autowired
    private ManageUserCache manageUserCache;

    @Override
    public ResultVO add(MeetingAddDTO params) {
        ResultVO resultVO = ResultVO.success();
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }

        LoginManageResultVO userInfo = manageUserCache.getLoginManageUserInfo();

        MeetingTypeDetailQueryDTO queryMeetingTypeDto = new MeetingTypeDetailQueryDTO();
        queryMeetingTypeDto.setId(params.getChangxingId());
        MeetingTypeDetailQueryResultDTO detail = meetingTypeService.getDetail(queryMeetingTypeDto);
        if (null == detail) {
            return ResultVO.fail("没有此会场类型");
        }

        YwHuichang add = new YwHuichang();
        BeanUtils.copyProperties(params, add);
        add.setChangxingbianhao(params.getHuichangbianhao());
        add.setChangxingmingcheng(params.getHuichangmingcheng());

        add.setJiudianId(userInfo.getJiudianId());
        add.setChuangjianrenId(userInfo.getId());
        add.setChuangjianshijian(new Date());
        add.setGengxinrenId(userInfo.getId());
        add.setGengxinshijian(new Date());
        add.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
        add.setZhuangtai(SysDicConstant.ZHUANGTAI_YOU_XIAO);
        int count = ywHuichangMapper.insertSelective(add);
        if (count == 0) {
            resultVO = ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    public ResultVO modify(MeetingModifyDTO params) {
        ResultVO resultVO = ResultVO.success();
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }

        MeetingTypeDetailQueryDTO queryMeetingTypeDto = new MeetingTypeDetailQueryDTO();
        queryMeetingTypeDto.setId(params.getChangxingId());
        MeetingTypeDetailQueryResultDTO detail = meetingTypeService.getDetail(queryMeetingTypeDto);
        if (null == detail) {
            return ResultVO.fail("没有此会场类型");
        }

        YwHuichang modify = new YwHuichang();
        BeanUtils.copyProperties(params, modify);
        modify.setChangxingbianhao(params.getHuichangbianhao());
        modify.setChangxingmingcheng(params.getHuichangmingcheng());
        modify.setGengxinrenId(manageUserCache.getCurrentUserId());
        modify.setGengxinshijian(new Date());
        int count = ywHuichangMapper.updateByPrimaryKeySelective(modify);
        if (count == 0) {
            resultVO = ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    public ResultVO delete(MeetingDelDTO delQueryDTO) {
        ResultVO resultVO = ResultVO.success();
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }

        YwHuichang delete = new YwHuichang();
        delete.setId(delQueryDTO.getId());
        delete.setShanchubiaoshi(SysDicConstant.DELFLAG_1);
        delete.setGengxinrenId(manageUserCache.getCurrentUserId());
        delete.setGengxinshijian(new Date());
        int count = ywHuichangMapper.updateByPrimaryKeySelective(delete);
        if (count == 0) {
            resultVO = ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    public RowsDataVO<MeetingQueryResultDTO> getUnderHotel(MeetingQueryDTO params) {
        RowsDataVO<MeetingQueryResultDTO> dataVO = new RowsDataVO<>();
        QueryConditions conditions = new QueryConditions();

        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        conditions.addCondition("jiudianId", EConditionOperator.EQULS, manageUserCache.getJiudianId());
        if (null != params.getChangxingId()) {
            conditions.addCondition("changxingId", EConditionOperator.EQULS, params.getChangxingId());
        }
        if (StringUtils.isNotBlank(params.getHuichangmingcheng())) {
            conditions.addCondition("huichangmingcheng", EConditionOperator.LIKE, params.getHuichangmingcheng());
        }

        MyExample example = new MyExample(YwHuichang.class, conditions, params.getOrderFields());
        Page<MeetingQueryResultDTO> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<YwHuichang> list = ywHuichangMapper.selectByExample(example);

        List<MeetingQueryResultDTO> resultList = list.stream().map(n -> {
            MeetingQueryResultDTO dto = new MeetingQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            dto.setZhuangtaiMingCheng(SysCodeUtil.getSysParamDesc(SysDicConstant.ZHUANGTAI, dto.getZhuangtai()));
            return dto;
        }).collect(Collectors.toList());

        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(resultList);
        return dataVO;
    }

    @Override
    public MeetingDetailQueryResultDTO getDetail(MeetingDetailDTO detailQueryDTO) {
        YwHuichang getDetail = new YwHuichang();
        getDetail.setId(detailQueryDTO.getId());
        YwHuichang ywHuichang = ywHuichangMapper.selectByPrimaryKey(getDetail);

        MeetingDetailQueryResultDTO dto = new MeetingDetailQueryResultDTO();
        BeanUtils.copyProperties(ywHuichang, dto);
        dto.setZhuangtaiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.ZHUANGTAI, dto.getZhuangtai()));
        dto.setJiedaidengjiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.JIEDAIDENGJI, dto.getJiedaidengji()));
        return dto;
    }

    @Override
    public RowsDataVO<MeetingUnusedQueryResultDTO> getMeetingUnusedList(MeetingUnusedQueryDTO entity) {
        RowsDataVO<MeetingUnusedQueryResultDTO> rowsDataVO = new RowsDataVO<MeetingUnusedQueryResultDTO>();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("jiudianid", manageUserCache.getJiudianId());
        paramMap.put("kaishishijian", entity.getKaishishijian());
        paramMap.put("jieshushijian", entity.getJieshushijian());
        if (entity.getLoudongId() != null) {
            paramMap.put("loudongid", entity.getLoudongId());
        }
        if (entity.getLoucengId() != null) {
            paramMap.put("loucengid", entity.getLoucengId());
        }
        if (entity.getChangxingId() != null) {
            paramMap.put("fangxingid", entity.getChangxingId());
        }
        if (entity.getZhuangtai() != null) {
            paramMap.put("zhuangtai", entity.getZhuangtai());
        }

        Page<MeetingUnusedQueryResultDTO> page = PageHelper.startPage(entity.getPageNo(), entity.getPageSize());
        List<MeetingUnusedQueryResultDTO> huichangUnusedQueryResultDTOList = ywHuichangMapper.getMeetingUnusedList(paramMap);

        rowsDataVO.setRows(huichangUnusedQueryResultDTOList);
        rowsDataVO.setPageNo(page.getPageNum());
        rowsDataVO.setTotalPages(page.getTotal());
        rowsDataVO.setTotalRecords(page.getPages());
        return rowsDataVO;
    }
}
